<?php
//filename : nilaidp3.php
require_once APPPATH.'controllers/gnc_mng'.EXT;

class NILAIDP3 extends Gnc_mng {
	protected $model_path	= 'dpt/m_penilai_dp3';
	protected $model_dp3	= 'dpt/m_dp3';
	protected $model_pegawai = 'dpt/m_pegawai';
	protected $model_nilai = 'dpt/m_nilai_dp3_rinci';
	protected $model_detail_dp3 = 'dpt/m_d_dpt';
	protected $model_header_dp3 = 'dpt/m_penilaih_dp3';

	function index()
	{
		// get all user data
		$namauser =  $this->session->userdata('user');
		$record = $this->load->model($this->model_pegawai)->select_query(array('nip'),array('key_sys_user' => $namauser),null);
		$nip = $record[0]->nip;
	
		$content = $this->load->model($this->model_path)->select_karakteristik($nip);

		if ($this->load->model($this->model_path)->isDinilai($nip, $nip_penilai) == true){
		
		}
		$data = array(
			'application' => $prefix,
			'application_title' => $this->common->get_application_description($prefix),
			'page' => 'Daftar Pegawai',
			'navigation' => $this->load->view('dpt/nav/general', '', TRUE),
			'content' => $this->load->view('dpt/viewer/penilai', array('content'=>$content), TRUE),
		);	// data yang akan dipassing ke view

		$this->load->view('layout/default', $data);	
	}
		
	function penilaian(){
		// get model details
		$id = $this->uri->segment(4, 0);
		$namauser =  $this->session->userdata('user');
		$record = $this->load->model($this->model_pegawai)->select_query(array('nip'),array('key_sys_user' => $namauser),null);
		$nip = $record[0]->nip;
		
		$record = $this->load->model($this->model_path)->select_karakteristik($nip);
		$content = $this->load->model($this->model_pegawai)->select_query(null,array('nip' => $nip),null);
		$ket = $this->load->model($this->model_dp3)->select_query();
		$pegawai = $this->load->model($this->model_pegawai)->select_query(null,array('nip' => $id),null);
		
		$data = array(
			'application' => $prefix,
			'application_title' => $this->common->get_application_description($prefix),
			'page' => 'Penilaian DP3',
			'navigation' => $this->load->view('dpt/nav/general', '', TRUE),
			'content' =>  $this->load->view('dpt/form/formulir_dp3', array('record'=>$record[0],'ket'=>$ket,'pegawai'=>$pegawai[0],'content'=>$content[0]), TRUE),
		);	// data yang akan dipassing ke view
		
		$this->load->view('layout/default', $data);	
		
	}

	function process_add()
	{
		for($i=0;$i<=12;$i++)
		{
			$opt[$i] = $_POST['opt'.$i];
		}
		
		$jlh = $_POST['jumlah'];
		$nip = $_POST['nip'];
		$nip_atasan = $_POST['nip_atasan'];
		$education = $_POST['pendidikan'];
		$jabatan = $_POST['jabatan'];
		$periode_dp3 = $_POST['periode_dp3'];
		$today = unix_to_mysql(now());
		

		$namauser =  $this->session->userdata('user');
		$record = $this->load->model($this->model_pegawai)->select_query(array('nip'),array('key_sys_user' => $namauser),null);
		$nip_penilai = $record[0]->nip;

		$datadp3 = $this->load->model($this->model_header_dp3)->select_query(array('id'),array(
			'nip' => $nip,
			'nip_penilai' => $nip_atasan,
		));
		$id_dp3 = $datadp3[0]->id;
		
		if ($this->load->model($this->model_path)->isPenilai_1($nip, $nip_penilai) == true)
		{		
			//echo "penilai 1";
			$insertdetail = array(
					'id_dp3' => $id_dp3,
					'tgl_penilaian'=> $today,
					'acc_nip' => $nip,
					'acc_penilai' => $nip_atasan,
					'dp3_11' =>$opt[0],
					'dp3_12' =>$opt[1],
					'dp3_13' =>$opt[2],
					'dp3_14' =>$opt[3],
					'dp3_15' =>$opt[4],
					'dp3_21' =>$opt[5],
					'dp3_22' =>$opt[6],
					'dp3_23' =>$opt[7],
					'dp3_24' =>$opt[8],
					'dp3_25' =>$opt[9],
					'dp3_26' =>$opt[10],
					'dp3_31' =>$opt[11],
					'dp3_32' =>$opt[12],
					'nilai'=>$jlh,
			);

			$insertdata = array(
					'nip'=>$nip,
					'jabatan'=>$jabatan,
					'pendidikan'=>$education,
					'nip1_penilai'=>$nip_atasan,
					'nilai1'=>$jlh,
			);				
			
			$model = $this->load->model($this->model_nilai);
			$model_detail = $this->load->model($this->model_detail_dp3);
			$this->common->who_am_i($insertdata, true, 'crea');
			$this->common->who_am_i($insertdetail, true, 'crea');
			$model->add($insertdata);
			$model_detail->add($insertdetail);
			$this->db->query("UPDATE dpt_h_dp3 SET STATUS = 'OK' WHERE nip ='$nip' AND periode_dp3 = '$periode_dp3'");
		}
		else if ($this->load->model($this->model_path)->isPenilai_2($nip, $nip_penilai) == true)
		{
			//echo "penilai 2";
			$insertdetail = array(
					'id_dp3' => $id_dp3,
					'tgl_penilaian'=> $today,
					'acc_nip' => $nip,
					'acc_penilai' => $nip_atasan,
					'dp3-11' =>$opt[0],
					'dp3-12' =>$opt[1],
					'dp3-13' =>$opt[2],
					'dp3-14' =>$opt[3],
					'dp3-15' =>$opt[4],
					'dp3-21' =>$opt[5],
					'dp3-22' =>$opt[6],
					'dp3-23' =>$opt[7],
					'dp3-24' =>$opt[8],
					'dp3-25' =>$opt[9],
					'dp3-26' =>$opt[10],
					'dp3-31' =>$opt[11],
					'dp3-32' =>$opt[12],
					'nilai'=>$jlh,
			);

			$insertdata = array(
					'nip'=>$nip,
					'jabatan'=>$jabatan,
					'pendidikan'=>$education,
					'nip2_penilai'=>$nip_atasan,
					'nilai2'=>$jlh,
			);				
			
			$model = $this->load->model($this->model_nilai);
			$model_detail = $this->load->model($this->model_detail_dp3);
			$this->common->who_am_i($insertdata, true, 'crea');
			$this->common->who_am_i($insertdetail, true, 'crea');
			$model->add($insertdata);
			$model_detail->add($insertdetail);
			$this->db->query("UPDATE dpt_h_dp3 SET STATUS = 'OK' WHERE nip ='$nip' AND periode_dp3 = '$periode_dp3'");		
		}
		else if ($this->load->model($this->model_path)->isPenilai_3($nip, $nip_penilai) == true)
		{
			//echo "penilai 3";
			$insertdetail = array(
					'id_dp3' => $id_dp3,
					'tgl_penilaian'=> $today,
					'acc_nip' => $nip,
					'acc_penilai' => $nip_atasan,
					'dp3-11' =>$opt[0],
					'dp3-12' =>$opt[1],
					'dp3-13' =>$opt[2],
					'dp3-14' =>$opt[3],
					'dp3-15' =>$opt[4],
					'dp3-21' =>$opt[5],
					'dp3-22' =>$opt[6],
					'dp3-23' =>$opt[7],
					'dp3-24' =>$opt[8],
					'dp3-25' =>$opt[9],
					'dp3-26' =>$opt[10],
					'dp3-31' =>$opt[11],
					'dp3-32' =>$opt[12],
					'nilai'=>$jlh,
			);

			$insertdata = array(
					'nip'=>$nip,
					'jabatan'=>$jabatan,
					'pendidikan'=>$education,
					'nip3_penilai'=>$nip_atasan,
					'nilai3'=>$jlh,
			);				
			
			$model = $this->load->model($this->model_nilai);
			$model_detail = $this->load->model($this->model_detail_dp3);
			$this->common->who_am_i($insertdata, true, 'crea');
			$this->common->who_am_i($insertdetail, true, 'crea');
			$model->add($insertdata);
			$model_detail->add($insertdetail);
			$this->db->query("UPDATE dpt_h_dp3 SET STATUS = 'OK' WHERE nip ='$nip' AND periode_dp3 = '$periode_dp3'");
		}
								
		$data = array(
			'application' => $prefix,
			'application_title' => $this->common->get_application_description($prefix),
			'page' => 'Nilai DP3',
			'navigation' => $this->load->view('dpt/nav/general', '', TRUE),
			'content' =>   $this->index(),
		);	// data yang akan dipassing ke view
		
		$this->load->view('layout/default', $data);	
	}

	
}